<template>
  <div class="app-container" v-loading="loading">
    <div class="top-operate">
      <div class="page-title"></div>
      <div class="button-box">
        <el-button type="primary" plain size="medium" @click.stop="cancel">取消</el-button>
        <el-button size="medium" @click.stop="saveForm"
          :disabled="!this.form.sysObjId1 || !this.form.sysObjId2">确定</el-button>
      </div>
    </div>
    <!--  顶部可切换同步方和接收方位置  -->
    <div class="schema-list">
      <el-carousel height="200px" :autoplay="false">
        <el-carousel-item >
          <div class="schema-basic-info">
            <div class="schema-detail">
              <div class="app-item">
                <div class="app-img">
                  <img src="@/assets/images/jindie.png" v-if="sourceSys.name === '金蝶云星空'">
                  <img src="@/assets/images/serialNum.jpg" v-else>
                </div>
                <div class="app-info">
                  <div class="app-name">{{sourceSys.name}}</div>
                  <div class="app-vers">类型: {{sourceSys.type}}</div>
                </div>
              </div>
              <div class="exchange-icon-box" @click.stop="tabSourceSys">
                <svg-icon icon-class="exchange" style="height: 38px;width: 87px;"/>
              </div>
              <div class="app-item">
                <div class="app-img">
                  <img src="@/assets/images/jindie.png" v-if="targetSys.name === '金蝶云星空'">
                  <img src="@/assets/images/serialNum.jpg" v-else>
                </div>
                <div class="app-info">
                  <div class="app-name">{{targetSys.name}}</div>
                  <div class="app-vers">类型: {{targetSys.type}}</div>
                </div>
              </div>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="flex-wrapper">
      <div class="bill-list source-bill-list">
        <div class="list-title">选择源系统对象</div>
        <div class="bill-list-box">
          <div class="filter-input-box">
            <el-input
              placeholder="输入对象名称进行筛选"
              v-model="sourceSearch"
              >
              <i slot="suffix" class="el-icon-search el-input__icon" />
            </el-input>
          </div>
          <el-radio-group v-model="sourceBusinessObject">
            <p v-for="(item, index) in sourceObject" :key="item.id">
              <el-radio
                :value="item.businessObjectName"
                :label="item.id"
              >
                {{item.businessObjectName}}
              </el-radio>
              <span v-if="item.businessType === '01'" class="bill-type bill">单据</span>
              <span v-if="item.businessType === '02'" class="bill-type basic">资料</span>
            </p>
          </el-radio-group>
        </div>
      </div>
      <div class="bill-list source-bill-list">
        <div class="list-title">选择目标系统对象</div>
        <div class="bill-list-box">
          <div class="filter-input-box">
            <el-input
              placeholder="输入对象名称进行筛选"
              v-model="targetSearch"
              >
              <i slot="suffix" class="el-icon-search el-input__icon" />
            </el-input>
          </div>
          <el-radio-group v-model="targetBusinessObject">
            <p v-for="(item, index) in targetObject" :key="item.id">
              <el-radio
                :value="item.businessObjectName"
                :label="item.id"
              >
                {{item.businessObjectName}}
              </el-radio>
              <span v-if="item.businessType === '01'" class="bill-type bill">单据</span>
              <span v-if="item.businessType === '02'" class="bill-type basic">资料</span>
            </p>
          </el-radio-group>
        </div>
      </div>
      <div class="add-btn" @click.stop="addSceneObject">
        <div>添加</div>
        <img src="">
      </div>
      <div class="bill-list selected-scene-list">
        <div class="list-title">
          源系统对象-目标系统对象
        </div>
        <div v-if="form.sysObjId1 && form.sysObjId2" class="scene-list">
          <el-form :inline="true" :model="form"  ref="form" label-width="110px">
            <div class="scene-item">
              <el-form-item label="集成场景">
                <el-input v-model="form.sceneConfig" disabled>
                </el-input>
              </el-form-item>
              <div class="scene-item-bottom">
                <div class="item-content">
                  <p>集成对象:{{form.sceneConfig}}</p>
                  <p>源-目标系统：{{sourceSys.name}}-{{targetSys.name}}</p>
                </div>
                <el-button type="text" @click.stop="removeSceneObject">删除</el-button>
              </div>
            </div>
          </el-form>
        </div>
        <el-empty v-else></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import { getSourceAndTargetSysObject } from '@/api/interfaceConfig/businessObject/businessObject'
import {addSceneConfig} from "@/api/interfaceConfig/sceneConfig/sceneConfig";
export default {
  name: "AddSceneConfig",
  data() {
    return {
      sourceSys: {
        name: '金蝶云星空',
        type: '通用'
      },
      targetSys: {
        name: '序列号追溯系统',
        type: '标准'
      },
      sourceObject: [],
      targetObject: [],
      tempSourceObject: [],
      tempTargetObject: [],
      //选择的源对象
      sourceBusinessObject: null,
      targetBusinessObject: null,
      form: {
        sysObjId1: null,
        sysObjId2: null,
        sceneConfig: ''
      },
      sourceSearch: '',
      targetSearch: '',
      loading: false
    }
  },
  watch: {
    'sourceSearch': {
      handler(newVal, oldVal) {
        this.sourceObject = this.tempSourceObject.filter(item => {
          item.includes(newVal)
        })
      }
    },
    'targetSearch': {
      handler(newVal, oldVal) {
        this.targetObject = this.tempTargetObject.filter(item => {
          item.includes(newVal)
        })
      }
    }
  },
  mounted() {
    this.getSourceAndTargetSysObject()
  },
  methods: {
    async getSourceAndTargetSysObject() {
      let resp = await getSourceAndTargetSysObject()
      this.sourceObject = resp.data.sourceObject
      this.tempSourceObject = resp.data.sourceObject
      this.tempTargetObject = resp.data.targetObject
      this.targetObject = resp.data.targetObject
    },
    //切换源和目标系统
    tabSourceSys() {
      let temp = this.sourceSys
      this.sourceSys = {...this.targetSys}
      this.targetSys = {...temp}
      this.reset()
      //交换
      let tempObjects = [...this.tempTargetObject]
      this.tempTargetObject = [...this.tempSourceObject]
      this.tempSourceObject = tempObjects
      this.sourceObject = this.tempSourceObject
      this.targetObject = this.tempTargetObject
    },
    reset() {
      this.form = {
        sysObjId1: null,
        sysObjId2: null,
        sceneConfig: ''
      }
      this.sourceBusinessObject = null
      this.targetBusinessObject = null
      this.sourceSearch = ''
      this.targetSearch = ''
    },
    addSceneObject() {
      if(!this.sourceBusinessObject || !this.targetBusinessObject) {
        this.$modal.msgWarning('请选择系统单据对象')
        return
      }
      //根据ID找到对象名称
      let sourceObj = this.findBusinessObjectName(this.sourceBusinessObject, this.tempSourceObject)
      let targetObj = this.findBusinessObjectName(this.targetBusinessObject, this.tempTargetObject)
      this.form = {
        sysObjId1: this.sourceBusinessObject,
        sysObjId2: this.targetBusinessObject,
        sceneConfig: sourceObj.businessObjectName + '-' + targetObj.businessObjectName
      }
    },
    removeSceneObject() {
      this.form = {
        sysObjId1: null,
        sysObjId2: null,
        sceneConfig: ''
      }
    },
    findBusinessObjectName(id, array = []) {
      return array.find(item => item.id === id)
    },
    cancel() {
      const obj = { path: "/interfaceConfig/SceneConfig" };
      this.$tab.closeOpenPage(obj);
    },
    async saveForm() {
      if(this.form.sysObjId1 && this.form.sysObjId2) {
        this.loading = true
        let resp = await addSceneConfig(this.form)
        this.$modal.msgSuccess("场景保存成功,正在跳转..");
        //路由到字段映射页面
        this.$router.push({
          path: '/sceneConfig/sceneFieldSetting',
          query: {
            id: resp.data
          }
        })
        this.loading = false
      } else {
        this.$modal.msgWarning('未选择场景对象，无法保存~')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top-operate {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  .page-title {
    line-height: 40px;
    color: #092048;
    font-size: 24px;
    font-weight: 500;
  }
  .button-box {
    display: flex;
  }
}

::v-deep .el-carousel__container{
  height: 150px !important;
  border-bottom: 1px solid #ccc;
  .carousel-box .el-carousel .el-carousel__indicators {
    display: none;
  }
  &:hover > .el-carousel__arrow--left,.el-carousel__arrow--right{
    display: none;
  }
}
.schema-basic-info {
  width: 80%;
  margin: 0 auto;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  .schema-detail {
    width: 100%;
    margin: 10px 0 10px;
    display: flex;
    justify-content: space-around;
    -webkit-box-align: center;
    align-items: center;
    .app-item {
      width: 260px;
      height: 100px;
      background: linear-gradient(122deg, #65b1ff 0%, #4a79ff 100%);
      border-radius: 4px;
      display: flex;
      -webkit-box-align: center;
      align-items: center;
      .app-img {
        width: 76px;
        height: 76px;
        margin-left: 15px;
        border-radius: 50%;
        background: #fff;
        display: flex;
        -ms-flex-negative: 0;
        flex-shrink: 0;
        -webkit-box-pack: center;
        justify-content: center;
        -webkit-box-align: center;
        align-items: center;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .app-info {
        margin-left: 18px;
        padding-right: 10px;
        word-break: break-all;
        color: #fff;
        .app-vers {
          font-size: 14px;
          margin: 5px 0;
        }
      }
    }
    .exchange-icon-box {
      text-align: center;
      cursor: pointer;
    }
  }
}

.flex-wrapper{
  display: flex;
  justify-content: space-between;
  .bill-list {
    width: 340px;
    padding: 10px 0;
    .list-title {
      margin: 20px 10px;
      font-size: 16px;
      font-weight: 500;
      color: #092048;

    }
    .bill-list-box {
      border: 1px solid #eee;
      .filter-input-box {
        height: 60px;
        padding: 10px;
        background: #f8f8f8;
        border-radius: 2px;
      }
      ::v-deep {
        .el-radio-group {
          max-height: 500px;
          overflow-y: auto;
          width: 100%;
          padding: 10px 0;

          p {
            height: 40px;
            display: flex;
            -webkit-box-align: center;
            align-items: center;
            -webkit-box-pack: justify;
            justify-content: space-between;
            padding: 0 20px;
            .bill-type {
              width: 54px;
              height: 24px;
              display: inline-block;
              font-size: 13px;
              border-radius: 2px;
              line-height: 24px;
              text-align: center;
            }

            .bill-type.basic {
              background: #deffc5;
              color: #3ac03e;
              border: 1px solid #69da6c;
            }
            .bill-type.bill {
              background: rgba(250, 169, 67, 0.21);
              color: #faa943;
              border: 1px solid #faa943;
            }
          }
        }
      }
    }
  }
  .add-btn {
    align-self: flex-start;
    margin-top: 85px;
    width: 80px;
    text-align: center;
    cursor: pointer;
  }
  .selected-scene-list {
    width: 330px;
  }
  .scene-list{
    .scene-item {
      &:last-child {
        border: none;
      }
      ::v-deep {
        .el-form-item {
          margin: 0;
        }
      }
      .scene-item-bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .item-content {
          font-size: 14px;
          font-weight: 400;
          color: #092048;
        }
        .el-button {
          margin-right: 10px;
        }
      }
    }
  }
}
</style>
